package com.trg.designmodel.singleleton;

/**
 * @ClassName: SingleLetonTest03
 * @Description: 单例设计模式-懒汉式（线程不安全）
 * 懒汉式，故名思意，比较懒，默认的时候不加载，只有在被调用的时候才会加载
 * @Author: tangrg
 * @Date: 2021/6/30 15:19
 * @Version: 1.0
 * @Return:
 **/
public class SingleLetonTest03 {
    public static void main(String[] args) {
        SingleLeton03 instance = SingleLeton03.getInstance();
        SingleLeton03 instance2 = SingleLeton03.getInstance();
        System.out.println(instance == instance2);
        System.out.println("instance:" + instance.hashCode());
        System.out.println("instance2:" + instance2.hashCode());
    }
}

class SingleLeton03 {

    //***1、私有化构造器，防止外部类new
    private SingleLeton03() {
    }

    private static SingleLeton03 instance;

    //***3、提供一个公有的静态方法，将实例对象返回
    public static SingleLeton03 getInstance() {
        //**** 只有在被调用的时候才会进行创建，否则不创建
        if (instance == null) {
            instance = new SingleLeton03();
        }
        return instance;
    }
}
